package com.qqtech.qquan.circle.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.circle.dao.CircleCategoryDao;
import com.qqtech.qquan.circle.model.CircleCategory;
import com.qqtech.qquan.circle.model.CircleCategoryQuery;
import com.qqtech.qquan.circle.model.CircleCategoryVo;

@Service
public class CircleCategoryServiceImpl extends BaseServiceImpl<CircleCategory>
		implements CircleCategoryService {
	public static final Logger log = LoggerFactory.getLogger(CircleCategoryServiceImpl.class);
	
	@Resource
	private CircleCategoryDao circleCategoryDao;

	@Override
	protected BaseDao<CircleCategory> getBaseDao() {
		return circleCategoryDao;
	}

	@Override
	public List<CircleCategoryVo> queryLevle1List() {
		CircleCategoryQuery q = new CircleCategoryQuery();
		q.setParentId(0);
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.ASC, "oidx"));
		return circleCategoryDao.queryList(q, pageKit);
	}

	@Override
	public List<CircleCategoryVo> queryChildrenList(Integer parentId) {
		CircleCategoryQuery q = new CircleCategoryQuery();
		q.setParentId(parentId);
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.ASC, "oidx"));
		return circleCategoryDao.queryList(q,pageKit);
	}

	@Override
	public CircleCategoryVo queryParent(Integer categoryId) {
		CircleCategory category = circleCategoryDao.getById(categoryId);
		if(category!=null){
			Integer parentId = category.getParentId();
			return circleCategoryDao.getById(parentId);
		}
		return null;
	}

	@Override
	public String queryFullCategoryName(Integer categoryId) {
		String name = ""; 
		CircleCategory category = circleCategoryDao.getById(categoryId);
		for(int i=0;i<3;i++){
			if(category!=null){
				name = category.getName()+"/"+name;
				category = circleCategoryDao.getById(category.getParentId());
			}else{
				break;
			}
		}
		return StringUtil.isBlank(name)? "": name.substring(0,name.length()-1);
	}

	@Override
	public String queryParentName(Integer categoryId) {
		CircleCategory category = circleCategoryDao.getById(categoryId);
		if(category!=null){
			if(category.getLevel()==1){
				return category.getName();
			}else{
				CircleCategory pCategory = circleCategoryDao.getById(category.getParentId());
				if(pCategory!=null){
					return pCategory.getName();
				}
			}
			
		}
		return "";
	}
	
}